package com.hgsoft.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hgsoft.dao.TransferPfDao;
import com.hgsoft.entity.TransferPf;
import com.hgsoft.util.Order;
import com.hgsoft.util.Pager;
import com.hgsoft.util.Property;

@Service 
public class TransferPfService extends BaseService<TransferPf>{
	
	@Resource(type=TransferPfDao.class)
	public void setDao(TransferPfDao dao){
		super.setDao(dao);
	}
	
	@Override
	public TransferPfDao getDao() {
	    	return (TransferPfDao)super.getDao();
	}
	
	public void saveList(List<TransferPf> list) {
	    for (TransferPf temp : list) {
		this.save(temp);
	    }
	}
	
	public void delete(Date date) {
	    this.getDao().delete(date);
	}
	
	public boolean findByDate(Date date) {
	    List<TransferPf> list = getDao().findAll(Order.desc("id"), Property.eq("date", date));
	    if(list != null && !list.isEmpty()) {
		return true;
	    } else {
		return false;
	    }
	}
	
	public List<TransferPf> query(Pager pager, TransferPf query, String startTime, String endTime,String direction) {

		List<Property> properties = new ArrayList<Property>();

		if (query != null) {
			if (query.getDate() != null) {
				properties.add(Property.eq("date", query.getDate()));
			}
		}
		if(query.getTimeInterval()!=null && query.getTimeInterval().intValue()>0){
			properties.add(Property.eq("timeInterval", query.getTimeInterval().intValue()));
		}
		if(startTime != null && !"".equals(startTime) && endTime != null && !"".equals(endTime)) {
		    properties.add(Property.between("startTime", startTime, endTime));
		    properties.add(Property.between("endTime", startTime, endTime));
		}
		
		if("1".equals(direction)){
			properties.add(Property.eq("inLine", 1));
			properties.add(Property.eq("outLine", 2));
		}
		if("2".equals(direction)){
			properties.add(Property.eq("inLine", 2));
			properties.add(Property.eq("outLine", 1));
		}
		
		return getDao().findByPager(pager, new Order[]{Order.desc("id")}, properties.toArray(new Property[properties.size()]));
	}
}
